package com.hui.wanxin.web.common.OBS;

import com.obs.services.ObsClient;
import lombok.extern.slf4j.Slf4j;
import org.joda.time.DateTime;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.util.UUID;

/**
 * @author: 灰灰
 * @date: 2023年10月13日 21:19
 * @ClassName: ObsUtils
 * @Description: TODO
 */
@Component
@Slf4j
public class ObsUtils {

    private static String accessKey = OBSKey.accessKey;

    private static String secretKey = OBSKey.securityKey;

    private static String bucketName = OBSKey.bucketName;

    private static String endpoint = OBSKey.endPoint;



    public String upload(MultipartFile multipartFile) {

        ObsClient client = new ObsClient(accessKey, secretKey,endpoint);
        try {
            // 上传文件流。
            InputStream inputStream = multipartFile.getInputStream();
            String fileName = multipartFile.getOriginalFilename();
            String uuid = UUID.randomUUID().toString().replaceAll("-","");
            fileName = uuid+fileName;
            String timeUrl = new DateTime().toString("yyyy/MM/dd");
            fileName = timeUrl+"/"+fileName;

            client.putObject(OBSKey.bucketName,fileName,inputStream);
            client.close();
            String url = "https://"+bucketName+"."+endpoint+"/"+fileName;
            return url;
        }catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage());
        }
        return null;
    }


}
